package com.gitee.wsl.platform.thread


import com.gitee.wsl.func.Producer
import com.gitee.wsl.concurrent.executor.Executor
import kotlin.native.concurrent.Worker
import kotlinx.atomicfu.atomic
import kotlin.native.concurrent.ObsoleteWorkersApi

internal object DefaultWorkRunners {

    private val workerCount = atomic(0L)

    @OptIn(ObsoleteWorkersApi::class)
    private fun createWorker(): Worker {
        val newCount = workerCount.incrementAndGet()
        return Worker.start(name = "mobius-worker-$newCount")
    }

    @OptIn(ObsoleteWorkersApi::class)
     fun eventWorkRunnerProducer(): Producer<Executor> {
        return Producer { WorkRunners.nativeWorker(createWorker()) }
    }

    @OptIn(ObsoleteWorkersApi::class)
     fun effectWorkRunnerProducer(): Producer<Executor> {
        return Producer { WorkRunners.nativeWorker(createWorker()) }
    }
}
